
<template>
    <van-button
        v-show="inGaming"
        style="position: absolute; right: 0; top: 0; z-index: 99999"
        type="primary"
        @click="onExit"
        >退出游戏界面</van-button
    >
    <simple-frame-sync
        ref="SimpleFrameSyncGame"
        :inGaming="inGaming"
        v-if="inGaming && demoSimpleFrameSync"
    ></simple-frame-sync>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { Button, Toast } from "vant";
import { GameClient } from "../assets/shared/gameClient/gameClient";
import { IGameServerInfo } from "../assets/shared/gateClient/GameServer";
import { GateClient } from "../assets/shared/gateClient/gateClient";
import SimpleFrameSync from "./GameDemos/SimpleFrameSync.vue";

let gameClient: GameClient;

export default defineComponent({
    name: "GameView",
    props: {
        gateClient: {
            type: GateClient,
            required: true,
        },
        gameServerInfo: {
            type: Object,
            required: false,
        },
        inGaming: {
            type: Boolean,
            default: false,
        },
        demoSimpleFrameSync: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {};
    },
    watch: {
        inGaming: {
            async handler(newVal) {
                if (newVal) {
                    this.onStart();
                }
            },
        },
    },
    emits: {
        exit: null,
    },
    components: {
        [Button.name]: Button,
        SimpleFrameSync,
    },
    methods: {
        async onStart() {
            Toast("开始进入游戏!");
            var gameServerInfo = this.gameServerInfo as IGameServerInfo;
            const result = await this.gateClient.loginByCustomUserName(
                gameServerInfo,
                "testUser"
            );
            if (!result.succ) {
                console.error("登录失败:" + result.err);
                Toast("登录失败:" + result.err);
                return;
            }
            gameClient = window.getGameClient(gameServerInfo.serverWSUrl);
            gameClient.disconnectHandler = () => {
                Toast(
                    "服务器连接断开, " +
                    gameClient.reconnectWaitSec +
                    "秒后重连"
                );
                return true;
            };
            gameClient.onReconnectResult = (succ, err) => {
                if (succ) {
                    Toast("重新连上服务器!");
                } else {
                    Toast("重新服务器失败:" + err + "  请重新进入游戏!");
                }
            };
            const loginErr = await gameClient.login(result.data as string);
            if (loginErr) {
                console.error("登录失败:" + loginErr);
                Toast("登录失败:" + loginErr);
                return;
            }
            Toast("成功连上游戏服务器!");

            if (this.demoSimpleFrameSync) {
                var gameComp = this.$refs.SimpleFrameSyncGame as any;
                gameComp.startGame(gameClient);
            }
        },
        async onExit() {
            if (this.demoSimpleFrameSync) {
                var gameComp = this.$refs.SimpleFrameSyncGame as any;
                gameComp.stopGame();
            }
            Toast("离开游戏!");
            this.$emit("exit");
        },
    },
});
</script>